class Solution(object):
    def minFlipsMonoIncr(self, s):
        """
        :type s: str
        :rtype: int
        """
        n = len(s)
        DP = [0] * n
        DP[-1] = 1 if s[-1] == '0' else 0
        for i in range(n - 2, -1, -1):
            DP[i] = DP[i + 1] + 1 if s[i] == '0' else DP[i + 1]
        if DP[0] == 0 or DP[0] == n:
            return 0
        min_value = float('inf')
        step = 0
        for i in range(n):
            if s[i] == '1':
                min_value = min(min_value, step + DP[i])
                step += 1
        min_value = min(step, min_value)
        return min_value


data = Solution()
s = "010010111"
print(data.minFlipsMonoIncr(s))
